package com.tronsis.distribution.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.lzy.imagepicker.ImagePicker;
import com.tronsis.distribution.R;
import com.tronsis.distribution.activity.goods.GoodsDetailActivity;
import com.tronsis.distribution.bean.ProductBean;
import com.tronsis.distribution.biz.factory.ProductBizFactory;
import com.tronsis.distribution.http.Url;
import com.tronsis.distribution.util.GlideImageLoader;
import com.tronsis.library.adapter.recyclerview.CommonAdapter;
import com.tronsis.library.adapter.recyclerview.MultiItemTypeAdapter;
import com.tronsis.library.adapter.recyclerview.base.ViewHolder;
import com.tronsis.library.adapter.recyclerview.wrapper.LoadMoreWrapper;
import com.tronsis.library.fragment.BaseFragment;
import com.tronsis.library.http.ListBean;
import com.tronsis.library.http.UICallBack;
import com.tronsis.library.util.DisplayUtil;
import com.tronsis.library.util.SRFConfigUtil;
import com.tronsis.library.util.ToastUtil;
import com.tronsis.library.widget.MyDecoration;
import com.tronsis.library.widget.RecycleViewDivider;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * allen: 添加库存数量
 */
public class GoodsFragment extends BaseFragment {


    @BindView(R.id.rv_goods)
    RecyclerView mRvGoods;
    @BindView(R.id.srl_goods)
    SwipeRefreshLayout mSrl;
    private int mPage = 1;
    private int mSize = 15;
    private int mTotal;

    private List<ProductBean> mGoodsList = new ArrayList<>();
    private LoadMoreWrapper mLoadMoreWrapper;


    @Override
    protected View onCreateView() {
        View view = getActivity().getLayoutInflater().inflate(R.layout.fragment_goods, null);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void initData() {
        initRv();
        httpGetProduct(mPage, mSize, true);
    }

    private void initRv() {
        ImagePicker.getInstance().setImageLoader(new GlideImageLoader());
        SRFConfigUtil.initSwipeRefreshLayout(mSrl);
        mSrl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                httpGetProduct(mPage = 1, mSize, true);
            }
        });

        CommonAdapter<ProductBean> mAdapter = new CommonAdapter<ProductBean>(getContext(), mGoodsList, R.layout.item_goods) {
            @Override
            protected void convert(ViewHolder holder, final ProductBean bean, int position) {
                holder.setText(R.id.tv_title, bean.getName());
                if(TextUtils.isEmpty(bean.getStock())){
                    holder.setVisible(R.id.tv_amount,false);
                }else{
                    holder.setVisible(R.id.tv_amount,true);
                    holder.setText(R.id.tv_amount, "库存数量："+bean.getStock());
                }
                holder.setImageByUrl(mContext, R.id.iv_cover, Url.FILE_DOWNLOAD + bean.getCoverFid());

                holder.setOnClickListener(R.id.tv_add_cart, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        httpAddCart(bean.getId());
                    }
                });

            }
        };

        mAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                Intent intent = new Intent(getActivity(), GoodsDetailActivity.class);
                intent.putExtra("ProductBean", mGoodsList.get(position));
                startActivity(intent);
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });

        mLoadMoreWrapper = new LoadMoreWrapper(mAdapter);
        mLoadMoreWrapper.setLoadMoreView(R.layout.rv_load_more);
        mLoadMoreWrapper.setOnLoadMoreListener(new LoadMoreWrapper.OnLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                if (mGoodsList.size() % 15 == 0 && mGoodsList.size() > 0 && mGoodsList.size() < mTotal) {
                    mPage = (mGoodsList.size() / 15) + 1;
                    httpGetProduct(mPage, mSize, false);
                }
            }
        });
        int height = DisplayUtil.getInstance(getContext()).dp2px(14);
        int color = Color.parseColor("#F4F4F4");
        RecycleViewDivider decoration = new RecycleViewDivider(getContext(), MyDecoration.VERTICAL_LIST, height, color);
        mRvGoods.addItemDecoration(decoration);
        mRvGoods.setLayoutManager(new LinearLayoutManager(getContext()));
        mRvGoods.setAdapter(mLoadMoreWrapper);
    }


    private void httpGetProduct(int page, int size, final boolean isRefresh) {
        try {
            ProductBizFactory.createBiz().getProductList(getActivity(), page, size, new UICallBack() {
                @Override
                public void onStart() {
                    if (isRefresh) {
                        mSrl.setRefreshing(true);
                    }
                }

                @Override
                public void onRefresh(Object data) {
                    mSrl.setRefreshing(false);
                    ListBean listBean = (ListBean) data;
                    mTotal = listBean.getTotal();
                    List<ProductBean> list = listBean.getList();
                    if (isRefresh) {
                        mGoodsList.clear();
                        mGoodsList.addAll(list);
                    } else {
                        mGoodsList.addAll(list);
                    }
                    mLoadMoreWrapper.notifyDataSetChanged();
                }

                @Override
                public void onFailure(int status) {
                    mSrl.setRefreshing(false);

                }
            });
        } catch (Exception e) {
            mSrl.setRefreshing(false);
            e.printStackTrace();
        }
    }


    private void httpAddCart(String productId) {
        try {
            ProductBizFactory.createBiz().addCart(getActivity(), productId, new UICallBack() {
                @Override
                public void onStart() {
                    showLoadingDialog();
                }

                @Override
                public void onRefresh(Object data) {
                    ToastUtil.showMessage(mContext, "添加成功");
                    dismissLoadingDialog();
                }

                @Override
                public void onFailure(int status) {
                    dismissLoadingDialog();

                }
            });
        } catch (Exception e) {
            ToastUtil.showMessage(mContext, "添加失败");
            e.printStackTrace();
            dismissLoadingDialog();
        }
    }

}
